package bjtu.tjj.simpledy.utils;

import cn.hutool.core.codec.Base64;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class SHA256Utils {
    private final static MessageDigest SHA256;

    static {
        try {
            SHA256 = MessageDigest.getInstance("SHA-256");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @return 长为43的字符串
     */
    public static String encode(String data) {
        return encode(data.getBytes());
    }

    /**
     * @return 长为43的字符串
     */
    public static String encode(byte[] input) {
        byte[] hash = SHA256.digest(input);
        return Base64.encodeUrlSafe(hash);
    }
}
